package com.ruoyi.bzjgl.service.impl;

import com.ruoyi.bzjgl.domain.Bzjtable;
import com.ruoyi.bzjgl.domain.FileTable;
import com.ruoyi.bzjgl.domain.LegalCaseFlow;
import com.ruoyi.bzjgl.mapper.BackToPersonMapper;
import com.ruoyi.bzjgl.mapper.BzjtableMapper;
import com.ruoyi.bzjgl.mapper.FlowMapper;
import com.ruoyi.bzjgl.mapper.ForfeitureOfDepositMapper;
import com.ruoyi.bzjgl.service.IBackToPersonService;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BackToPersonServiceImpl implements IBackToPersonService {
    @Resource
    private BackToPersonMapper backToPersonMapper;
    @Resource
    private BzjtableMapper bzjtableMapper;
    @Resource
    private ForfeitureOfDepositMapper forfeitureOfDepositMapper;
    @Resource
    private FlowMapper flowMapper;

    @Override
    public List<Bzjtable> selectThglList(Bzjtable bzjtable) {
        return backToPersonMapper.selectThglList(bzjtable);
    }

    @Override
    public List<Bzjtable> selectThshList(Bzjtable bzjtable) {
        return backToPersonMapper.selectThshList(bzjtable);
    }

    @Override
    public List<Bzjtable> selectThhzList(Bzjtable bzjtable) {
        return backToPersonMapper.selectThhzList(bzjtable);
    }

    @Override
    public int updateThshzt(Bzjtable bzjtable) {
        if(bzjtable!=null){
            if(bzjtable.getXh()!=null){
                LoginUser loginUser = SecurityUtils.getLoginUser();
                forfeitureOfDepositMapper.deleteFileByBzjXh(bzjtable.getXh(),"退还");
                for (FileTable fileTable : bzjtable.getFiles()) {
                    if(fileTable.getFileFirstPath()!=null&&fileTable.getFileLastPath()!=null){
                        fileTable.setXh(bzjtable.getXh());
                        fileTable.setAjbh(bzjtable.getAjbh());
                        fileTable.setLb("退还");
                        fileTable.setWjdz(fileTable.getFileFirstPath()+fileTable.getFileLastPath());
                        fileTable.setCzy(loginUser.getUsername());
                        fileTable.setCzsj(new Date());
                        fileTable.setFjmc(fileTable.getOriginalFilename());
                        fileTable.setWjlx(fileTable.getFileType());
                    }
                    forfeitureOfDepositMapper.insertFileTable(fileTable);
                }
                bzjtable.setThtfr(loginUser.getUsername());
                bzjtable.setThtfsj(new Date());
                return bzjtableMapper.updateBzjtable(bzjtable);
            }
        }
        return 0;
    }

    @Override
    public int thsh(Bzjtable bzjtable) {
        if(bzjtable!=null){
            LoginUser loginUser = SecurityUtils.getLoginUser();
            bzjtable.setThtfr(loginUser.getUsername());
            bzjtable.setThtfsj(new Date());
            //处理流程
            LegalCaseFlow legalCaseFlow = new LegalCaseFlow();
            legalCaseFlow.setAjxh(bzjtable.getXh());
            if("1".equals(bzjtable.getThshzt())){
                legalCaseFlow.setAjlx("提交");
            }else if("2".equals(bzjtable.getThshzt())){
                legalCaseFlow.setAjlx("通过");
            }else if("3".equals(bzjtable.getThshzt())){
                legalCaseFlow.setAjlx("退回");
            }
            legalCaseFlow.setLb("退还");
            legalCaseFlow.setSpsj(new Date());
            legalCaseFlow.setSpr(bzjtable.getSpr());
            if(loginUser.getDeptId()!=null)
                legalCaseFlow.setSpdw(loginUser.getDeptId().toString());
            legalCaseFlow.setSpyj( bzjtable.getSpyj());
            flowMapper.insertFlow(legalCaseFlow);
            return bzjtableMapper.updateBzjtable(bzjtable);
        }else{
            return 0;
        }
    }

    @Override
    public int shBack(Bzjtable bzjtable) {
        if(bzjtable!=null){
            LoginUser loginUser = SecurityUtils.getLoginUser();
            bzjtable.setThshzt("3");
            bzjtable.setThtfr(loginUser.getUsername());
            bzjtable.setThtfsj(new Date());
            return bzjtableMapper.updateBzjtable(bzjtable);
        }else{
            return 0;
        }
    }

    @Override
    public Bzjtable selectThByXh(String xh) {
        Bzjtable bzjtable = bzjtableMapper.selectBzjtableByXh(xh);
        List<FileTable> fileTableList = forfeitureOfDepositMapper.selectFjByXhAndLb(xh,"退还");
        bzjtable.setFiles(fileTableList);
        return bzjtable;
    }


}
